package fx.iog.home;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import fx.iog.gen.R;

public class MenuDrawerAdapter extends BaseAdapter {
	
	private List<RowMenuDrawer> rowItems = new ArrayList<RowMenuDrawer>();
	private LayoutInflater Inflater;
	

	public MenuDrawerAdapter(Context context,int resImg, int resName) {
		
		
		Inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		TypedArray imgArray = context.getResources().obtainTypedArray(resImg);
		String[] nomeArray = context.getResources().getStringArray(resName);

		for (int i = 0; i < nomeArray.length; i++) {
			RowMenuDrawer item = new RowMenuDrawer(nomeArray[i],imgArray.getResourceId(i,-1));
			rowItems.add(item);
		}
	}

	@Override
	public int getCount() {
		return rowItems.size();
	}

	@Override
	public Object getItem(int position) {
		return rowItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return rowItems.indexOf(getItem(position));
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		ViewHolder holder = null;

		if (view == null) {

			view = Inflater.inflate(R.layout.drawer_list, null);

			holder = new ViewHolder();

			holder.imagemMenu = (ImageView) view
					.findViewById(R.id.img_drawermenu);
			
			holder.nameMenu = (TextView) view.findViewById(R.id.txt_drawermenu);
			
			//holder.imagePerfil = (ImageView) view.findViewById(R.id.img_perfildrawer);
			

			view.setTag(holder);

		} else {
			holder = (ViewHolder) view.getTag();
		}
		
		RowMenuDrawer rowItem = rowItems.get(position);
	
		holder.imagemMenu.setImageResource(rowItem.getImage());
		holder.nameMenu.setText(rowItem.getNome());
		
		if(position == 0){
			view = Inflater.inflate(R.layout.drawer_img_perfil, null);
			
			ImageView imagePerfil = (ImageView) view.findViewById(R.id.img_perfildrawer);
			
			imagePerfil.setBackgroundResource(R.drawable.jorginho);
		}
		return view;
	}

	/* private view holder class */
	private class ViewHolder {

		public ImageView imagemMenu;
		public TextView nameMenu;
		//public ImageView imagePerfil;

	}

}
